import { execa, execaSync } from 'execa';
import { cwd } from 'process';

class Executor {
    public static RunCmd(cmd: string): string {
        // const result = execaSync(cmd, [], { stdin: 0 });
        // return result.stdout;
        const { stdout } = execaSync(cmd, [], {
            stdin: 0,
            windowsVerbatimArguments: true,
            encoding: 'buffer',
        });

        // 直接用 StringDecoder 尝试转码（可能需要指定编码）
        const decoder = new TextDecoder('gbk');
        return decoder.decode(stdout);
    }

    public static RunCmdAsync(cmd: string) {
        const subprocess = execa(cmd, [], {stdin: 0});
    }

    public static RunScript(scriptPath: string, scriptFile: string): string {
        const { stdout } = execaSync(scriptFile, [], {
            stdin: 0,
            cwd: scriptPath,
            windowsVerbatimArguments: true,
            encoding: 'buffer',
        });

        // 直接用 StringDecoder 尝试转码（可能需要指定编码）
        const decoder = new TextDecoder('gbk');
        return decoder.decode(stdout);
    }

    public static RunScriptAsync(scriptPath: string, scriptFile: string) {
        const subprocess = execa(scriptFile, [], {stdin: 0, cwd: scriptPath});
    }
}

export default Executor;